/*
 * LineManager.h
 *
 *  Created on: 2012-9-9
 *      Author: yangentao@gmail.com
 */

#ifndef LINEMANAGER_H_
#define LINEMANAGER_H_

#include <assert.h>
#include <algorithm>
#include "../util/util.h"

using namespace std;

class lineListener{
public:
	virtual ~lineListener(){}
	virtual void onLineReplaced(int from, int to, int* newLines, int newLinesCount) = 0;
};

//external should store the line number rather than the position of the line
//line [0, count)
class LineManager {
protected:
	IntVector vec;
public:
	LineManager();
	void dump();
public:
	//how manay lines
	inline size_t count() {
		return vec.size() - 1; //sub the last element
	}
	//the start position of some line
	inline size_t getLineStart(size_t line) {
		if (line >= vec.size()) {
			throw IndexOutOfBoundException();
		}
		return vec[line];
	}
	//the position belongs which line
	size_t findLine(int position);
	void onTextReplaced(int from, int oldTextLength, const String& newText) ;
protected:
	void parseCrLf(const String& text, Vector<int>& lines, int delta);
};
#endif /* LINEMANAGER_H_ */
